﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UtilZ.Dotnet.AudioPlayer.Core;
using UtilZ.Dotnet.AudioPlayer.Player;
using UtilZ.Dotnet.AudioPlayer.Recorder;
using UtilZ.Dotnet.Ex.Audio;
using UtilZ.Dotnet.Ex.Log;

namespace ConAppCore
{
    internal class TestSoundPlayer
    {
        internal static void Test()
        {
            try
            {
                List<SoundPlayingDeviceInfo> soundPlayingDevices = SoundPlayerAbs.GetSoundDeviceInfos();
                List<RecordDeviceInfo> recordDeviceInfos = SoundRecorder.GetRecordDeviceInfos();
                SoundPlayerAbs.InitDevice(-1, 48000);
                BASSHelper.LoadPlugins();

                //TestFileSoundPlayer();
                TestStreamSoundPlayer();
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestStreamSoundPlayer()
        {
            string filePath = @"D:\Temp\FM\sdr.wav";
            //filePath = @"D:\HBData\bay_bay_bay_R.wav";
            filePath = @"D:\HBData\bay_bay_bay_32.wav";

            var fileWaveInfo = WaveHelper.GetWavHead(filePath);
            if (fileWaveInfo == null)
            {
                Console.WriteLine("无效的wave格式");
                return;
            }

            using var player = new StreamSoundPlayer(1, fileWaveInfo);

            {
                var headSize = WaveHelper.GetWaveHeadSize(fileWaveInfo.Encoding, fileWaveInfo.HasFact);
                byte[] buffer = File.ReadAllBytes(filePath);
                player.AppendData(buffer.Skip(headSize).ToArray());
            }

            player.Play();

            var xx = Console.ReadKey();
        }

        private static void TestFileSoundPlayer()
        {
            string filePath = @"D:\HBData\1\20240919\USW_ANA_VOC\20240919155724_HY_02BJWD50001001_F_0090000000_B_0000150000_S_0000008000_ms_685.wav";
            filePath = @"D:\Music\loney.mp3";
            filePath = @"D:\HBData\z_我爱你中国伴奏_a2p.wav";
            filePath = @"E:\Music\Flight_Of_The_Silverbird-Two_Steps_From_Hell_Thomas_Bergersen.flac";
            //filePath = @"E:\Music\张信哲-爱就一个字.ape";
            //filePath = @"E:\Music\Two Steps From Hell - Star Sky (纯音乐).mp3";
            filePath = @"D:\HBData\bay_bay_bay_32.wav";

            using var player = new FileSoundPlayer(1);
            //player.LoadFile(@"F:\Music\fxdd.flac");
            player.LoadFile(filePath);
            player.Play();

            var xx = Console.ReadKey();
        }

    }


}
